﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Core.Report.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.App.Data;




namespace HSXMBillService
{
    [Description("报表金额字段显示千分位")]
    //[Kingdee.BOS.Util.HotUpdate]
    public class HSXMEdit : AbstractSysReportPlugIn
    {

        public override void FormatCellValue(Kingdee.BOS.Core.Report.PlugIn.Args.FormatCellValueArgs args)
        {

            base.FormatCellValue(args);


            if (!args.Header.FieldName.EqualsIgnoreCase("FACCTBOOKNAME") && !args.Header.FieldName.EqualsIgnoreCase("FCURRENCYID") && !args.Header.FieldName.EqualsIgnoreCase("FYear") && !args.Header.FieldName.EqualsIgnoreCase("FPeriod")
                && !args.Header.FieldName.EqualsIgnoreCase("FDate") && !args.Header.FieldName.EqualsIgnoreCase("FZZNumber") && !args.Header.FieldName.EqualsIgnoreCase("FZZName") && !args.Header.FieldName.EqualsIgnoreCase("FSupNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FSupName") && !args.Header.FieldName.EqualsIgnoreCase("FCustNumber") && !args.Header.FieldName.EqualsIgnoreCase("FCustName") && !args.Header.FieldName.EqualsIgnoreCase("FEMPNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FEMPName") && !args.Header.FieldName.EqualsIgnoreCase("FAXMNumber") && !args.Header.FieldName.EqualsIgnoreCase("FAXMName") && !args.Header.FieldName.EqualsIgnoreCase("FBXMNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FBXMName") && !args.Header.FieldName.EqualsIgnoreCase("FADepNumber") && !args.Header.FieldName.EqualsIgnoreCase("FADepName") && !args.Header.FieldName.EqualsIgnoreCase("FBDepNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FBDepName") && !args.Header.FieldName.EqualsIgnoreCase("FCDepNumber") && !args.Header.FieldName.EqualsIgnoreCase("FCDepName") && !args.Header.FieldName.EqualsIgnoreCase("FDDepNumber")
                && !args.Header.FieldName.EqualsIgnoreCase("FDDepName") && !args.Header.FieldName.EqualsIgnoreCase("FDC") && !args.Header.FieldName.EqualsIgnoreCase("FID"))
                     {
                         args.FormateValue = String.Format("{0:N}", args.Value);
                     }
        
        }
        public override void OnFormatRowConditions(ReportFormatConditionArgs args)
        {

            base.OnFormatRowConditions(args);
            //行背景色：

            FormatCondition fc = new FormatCondition();

            if (args.DataRow["FEXPLANATION"].ToString().Contains("本年累计"))
            {

                fc.BackColor = "#97eeff";// 背景色蓝色;

            }
            if (args.DataRow["FEXPLANATION"].ToString().Contains("本期累计"))
            {

                fc.BackColor = "#97eeff";// 背景色蓝色;
                
            }
            args.FormatConditions.Add(fc);

         
        }
       /// <summary>
        /// 方案一，用户双击单元格：读取焦点单元格记录的单据编号，打开对应单据
        /// </summary>
        /// <param name="e"></param>
        /// <remarks>
        /// 单元格不需要显示为超链接格式;
        /// </remarks>
        /// 
        public override void CellDbClick(CellEventArgs Args)
        {
            Args.Cancel = true;
            int rowSeq = Args.CellRowIndex;     // 行序号，以1开始
            string fldKey = Args.Header.FieldName;
            // 根据当前所选的单据编号字段名，确定需要打开的单据类型
            // 示例代码，以打开采购订单为例
            string FACCOUNTBOOKID, FACCTORGID;
            FACCOUNTBOOKID = "";
            FACCTORGID = "";
            string formId = string.Empty;

            DynamicObject dyFilter = this.SysReportModel.FilterParameter.CustomFilter;

            DynamicObject FACCTBOOKID = dyFilter["FACCTBOOKID"] as DynamicObject;
            if (FACCTBOOKID != null)
            {
                FACCOUNTBOOKID = Convert.ToString(FACCTBOOKID["ID"]);
                FACCTORGID = Convert.ToString(FACCTBOOKID["AccountOrgID_Id"]);
            }

            if (fldKey.EqualsIgnoreCase("FVOUCHERGROUPNO"))
            {
                string FVOUCHERGROUPNO = this.GetBillNo("FVOUCHERGROUPNO", rowSeq - 1);
                string FDate = this.GetBillNo("FDate", rowSeq).ToString();
                string FVOUCHERGROUP = this.GetBillNo("FVOUCHERGROUP", rowSeq).ToString();

                string FSQLB = "/*dialect*/ SELECT FVOUCHERID FROM T_GL_VOUCHER T1 " +
               "LEFT JOIN T_BD_VOUCHERGROUP_L T2 ON T1.FVOUCHERGROUPID=T2.FVCHGROUPID " +
               "WHERE T2.FNAME='" + FVOUCHERGROUP + "' AND T1.FACCOUNTBOOKID =" + FACCOUNTBOOKID + " and t1.FDate ='" + FDate + "' " +
               "AND T1.FACCTORGID =" + FACCTORGID + " AND T1.FVOUCHERGROUPNO='" + FVOUCHERGROUPNO + "'";

                string pkValue = DBUtils.ExecuteScalar<string>(this.Context, FSQLB, string.Empty, null);
                formId = "GL_VOUCHER";

                // 根据单据编号，加载单据内码
                // 显示指定单据
                if (pkValue != "")
                {
                    this.ShowForm(formId, pkValue);
                }

            }
            if (string.IsNullOrWhiteSpace(formId))
            {
                // 双击的是普通单元格，不需要显示单据
                return;
            }
            string billNo = this.GetBillNo(fldKey, rowSeq - 1);
            if (string.IsNullOrWhiteSpace(billNo))
            {
                // 单元格无内容，不确定需要打开那张单据
                return;
            }



        }

        private string GetBillNo(string fldKey, int rowIndex)
        {
            // 方案一：使用行索引，到报表数据源中自行获取行
            // 报表显示的数据源
            // DataTable dt = ((ISysReportModel)this.View.Model).DataSource;
            //if (dt.Rows.Count == 0 || rowIndex >= dt.Rows.Count)
            //{
            //    return string.Empty;
            //}
            //DataRow currRow = dt.Rows[rowIndex];
            //string billNo = Convert.ToString(currRow[fldKey]);
            // 方案二：获取报表当前选择的数据行
            DataRow[] rows = ((ISysReportView)this.View).SelectedDataRows;
            string billNo = Convert.ToString(rows[0][fldKey]);
            return billNo;

        }

        private void ShowForm(string formId, string pkValue)
        {
            // using Kingdee.BOS.Core.Bill;
            string pageId = Guid.NewGuid().ToString();
            BillShowParameter showParameter = new BillShowParameter();
            showParameter.FormId = formId;
            showParameter.OpenStyle.ShowType = ShowType.Floating;
            showParameter.PageId = pageId;
            showParameter.Status = OperationStatus.EDIT;
            showParameter.PKey = pkValue;
            this.View.ShowForm(showParameter);
        }
       

    }
}
